/**
 * 
 */
package com.bohai.weixin.web.controller;

import java.io.IOException;
import java.util.Arrays;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.bohai.base.annotation.LoginNeedless;
import com.bohai.base.web.controller.BaseController;

/**
 * @author ricky
 *
 */
@LoginNeedless
@Controller
public class SignController extends BaseController{
	
	@RequestMapping(value="/message/weixin",method = RequestMethod.GET) 
	public void weixinsign(String signature,String timestamp,String nonce,String echostr,HttpServletRequest request,HttpServletResponse response) throws IOException{
		
		String Token = "bohaisoft";
		/*
		List<String> params = new ArrayList<String>();  
	    
		params.add(Token);  
	    params.add(timestamp);  
	    params.add(nonce);  
	    //1. 将token、timestamp、nonce三个参数进行字典序排序    
	    Collections.sort(params, new Comparator<String>() {  
	        @Override  
	        public int compare(String o1, String o2) {  
	            return o1.compareTo(o2);  
	        }  
	    });  
	    System.out.println(params);
	    logger.error(params.toString());
	    byte[] b = null;
		try {
			b = (params.get(0) + params.get(1) + params.get(2)).getBytes("utf-8");
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    //2. 将三个参数字符串拼接成一个字符串进行sha1加密    
	    String temp = DigestUtil.getFileMD5(params.get(0) + params.get(1) + params.get(2),"SHA-1");  
	    System.out.println(temp);
	    logger.error(temp);
	    if (temp.equals(signature)) {  
	    	//response.getWriter().write(echostr);  
	    	return echostr;
	    }  */
		
		
		String[] str = { Token, timestamp, nonce };
        Arrays.sort(str); // 字典序排序
        String bigStr = str[0] + str[1] + str[2];
        
        String digest = DigestUtils.shaHex(bigStr);
        // 确认请求来至微信
        if (digest.equals(signature)) {
            response.getWriter().print(echostr);
        	
        }
	    return;
	}
}
